#include "ObScoreTree.h"
#include "Collision.h"

IDirect3DTexture9* ObScoreTree::mTex		= 0;
IDirect3DTexture9* ObScoreTree::mFlagTex	= 0;

ObScoreTree::ObScoreTree(){
	setId(FLAGTREE);
	mInfo	= 0;
}
ObScoreTree::~ObScoreTree(){

}

bool ObScoreTree::setId(int nid){
	if(checkType(nid)){
		mID = nid;
		mWidth	= 2.0f;
		mHeight = 160.0f;
		mCenter = D3DXVECTOR3(8.0f, mHeight/2, 0.0f);
		mSpritePos.x = 8.0f;
		mSpritePos.y = 80.0f;
		mCurrentSprite = 0;
		mNeedCollision = true;
		mIsCatched = false;
		count = 0.0f;
		mFlagSpritePos = D3DXVECTOR3(10.0f, 8.0f, 0.0f);
		mFlagCenter = D3DXVECTOR3(8.0f, 8.0f, 0.0f);
		return true;
	}else{
		return false;
	}
}

bool ObScoreTree::checkType(int obID){
	if(obID == FLAGTREE){
		return true;
	}else{
		return false;
	}
}

void ObScoreTree::collidedWith(ObObject* object, CollisionRS rs){
	switch (object->getId())
	{
	case ObPlayer::MARIO:
		mNeedCollision = false;
		mIsCatched = true;
		break;
	}
}

void ObScoreTree::update(float dt){
	if(mNeedDispose == true) return;
	
	if(mIsCatched){
		count +=dt;
		if(count>= 0.2f){
			count = 0.0f;
			mCurrentSprite++;
			if(mCurrentSprite>=3 ){
				mCurrentSprite = 0;
			}
			mFlagSpritePos.x = 10.0f + mCurrentSprite*20;
		}
	}

}

void ObScoreTree::render(ID3DXSprite* mSprite){
	if(mID == 0) return;
	if(mTex == 0) return;
	if(mNeedDispose) return;

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x,mPos.y - gCamera->getPos().y,0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect = calRenderRECT(mSpritePos,16,mHeight);
	mSprite->Draw(mTex,&rect,&mCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));

	if(mIsCatched){
		drawFlag(mSprite);
	}
}

void ObScoreTree::drawFlag(ID3DXSprite* mSprite){
	if(mFlagTex == 0) return;

	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,true));
	D3DXMATRIX S;
	D3DXMatrixScaling(&S,1.0f,-1.0f,1.0f);
	D3DXMATRIX R;
	D3DXMatrixRotationZ(&R, mRotation);
	D3DXMATRIX T;
	D3DXMatrixTranslation(&T,mPos.x - gCamera->getPos().x+8,mPos.y - gCamera->getPos().y + 55,0);
	mSprite->SetTransform(&(S*R*T));
	RECT rect1 = calRenderRECT(mFlagSpritePos, 16, 16);
	mSprite->Draw(mFlagTex,&rect1,&mFlagCenter,0,D3DCOLOR_XRGB(255, 255, 255));
	mSprite->Flush();
	HR(gDevice->SetRenderState(D3DRS_ALPHATESTENABLE,false));
	
}

bool ObScoreTree::loadTexture(){
	if(mTex == 0){
		HR(D3DXCreateTextureFromFileEx(
			gDevice,
			"Sprites/ScoreTree.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mTex));
	}else{
		return false;
	}

	if(mFlagTex == 0){
		HR(D3DXCreateTextureFromFileEx(
			gDevice,
			"Sprites/ScoreTreeFlag.png",
			D3DX_DEFAULT_NONPOW2,
			D3DX_DEFAULT_NONPOW2,
			0,
			0,
			D3DFMT_UNKNOWN,
			D3DPOOL_MANAGED,
			D3DX_DEFAULT,
			D3DX_DEFAULT,
			D3DCOLOR_XRGB(0,0,0),
			NULL,
			NULL,
			&mFlagTex));
	}else{
		return false;
	}

	return true;
}
bool ObScoreTree::releaseTexture(){
	if(mTex!=0){
		ReleaseCOM(mTex);
	}else{
		return false;
	}

	if(mFlagTex!=0){
		ReleaseCOM(mFlagTex);
	}else{
		return false;
	}

	return true;
}